package com.module.aichat.db

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.module.aichat.db.table.DBMessage
import kotlinx.coroutines.flow.Flow

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/6/30
 * <p>
 * 描述：会话列表的Dao
 * <p>
 * 修订历史：
 */
@Dao
interface ChatMessageDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(vararg bean: DBMessage)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(mList:List<DBMessage>)

    @Query("SELECT sendTime FROM chat_message WHERE sendTime IN (:times)")
    fun getExistingTimes(times: List<Long>): List<Long>

    @Query("SELECT * FROM chat_message")
    fun findAll(): MutableList<DBMessage>

    @Query("SELECT * FROM chat_message")
    fun findAllFlow(): Flow<List<DBMessage>>

    // 按sendTime升序排列查询所有数据
    @Query("SELECT * FROM chat_message ORDER BY sendTime ASC")
    fun findAllOrderByTimeAsc(): MutableList<DBMessage>

    // 按sendTime降序排列查询所有数据
    @Query("SELECT * FROM chat_message ORDER BY sendTime DESC")
    fun findAllOrderByTimeDesc(): MutableList<DBMessage>

    // 查询 所有  倒序 查询
    @Query("SELECT * FROM chat_message ORDER BY ID DESC  LIMIT :mSize")
    fun findByDescSize(mSize: Int): MutableList<DBMessage>

    //查询 所有sendStatus不等于指定状态的数据 按sendTime升序排列
    @Query("SELECT * FROM chat_message WHERE sendStatus != :status ORDER BY sendTime ASC")
    fun findAllByStatus(status: Int): MutableList<DBMessage>

    @Query("SELECT * FROM chat_message WHERE sendStatus != :status ORDER BY sendTime ASC")
    fun findAllByStatusFlow(status: Int): Flow<MutableList<DBMessage>>

    @Update
    fun update(msg: DBMessage)

    @Query("select count(*) from chat_message")
    fun count(): Int

    //查询某条数据
    @Query("SELECT * FROM chat_message WHERE sendTime = :time")
    fun findMessageByTime(time:Long): DBMessage

    // 先获取最新日期，再查询该日期的所有数据
    @Query("SELECT * FROM chat_message WHERE date(sendTime/1000, 'unixepoch', 'localtime') = (SELECT date(MAX(sendTime)/1000, 'unixepoch', 'localtime') FROM chat_message) ORDER BY sendTime DESC")
    fun findLatestDayRecords(): List<DBMessage>

    @Query("SELECT * FROM chat_message WHERE date(sendTime/1000, 'unixepoch', 'localtime') = (SELECT date(MAX(sendTime)/1000, 'unixepoch', 'localtime') FROM chat_message) ORDER BY sendTime DESC")
    fun findLatestDayFlow(): Flow<List<DBMessage>>

    //根据时间范围查询数据
    @Query("SELECT * FROM chat_message WHERE sendTime >= :startTime AND sendTime <= :endTime ORDER BY sendTime ASC")
    fun findByTimeRange(startTime: Long, endTime: Long): List<DBMessage>

    // 根据时间范围查询数据（Flow版本）
    @Query("SELECT * FROM chat_message WHERE sendTime >= :startTime AND sendTime <= :endTime ORDER BY sendTime ASC")
    fun findByTimeRangeFlow(startTime: Long, endTime: Long): Flow<List<DBMessage>>

    //删除所有 → '所有'的只能用@Query,@Query可以编写任意的sql语句
    @Query("DELETE FROM chat_message")
    fun deleteAll()

    @Query("DELETE FROM chat_message WHERE sendTime = :time")
    fun deleteMsgByTime(time:Long)

    @Delete
    fun delete(data: DBMessage)

    @Delete
    fun deleteEvents(list: MutableList<DBMessage>)
}